package cn.backpackerxl.easyJDBC.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class JSONObject {
    private int intValue;
    private String strValue;
    private boolean boolValue;
    private Map<String, JSONObject> objValue;
    private List<JSONObject> arrays;
    private double douValue;
    private long longValue;


    public JSONObject(int intValue) {
        this.intValue = intValue;
    }

    public JSONObject(long longValue) {
        this.longValue = longValue;
    }

    public JSONObject(double douValue) {
        this.douValue = douValue;
    }

    public JSONObject(String strValue) {
        this.strValue = strValue;
    }

    public JSONObject(boolean boolValue) {
        this.boolValue = boolValue;
    }

    public JSONObject(Map<String, JSONObject> objValue) {
        this.objValue = objValue;
    }

    public JSONObject(List<JSONObject> arrays) {
        this.arrays = arrays;
    }

    /**
     * 将字符串转化为日期
     *
     * @return 日期
     */
    public Date toDate() {
        if (this.longValue != 0) {
            return new Date(this.longValue);
        } else {
            throw new RuntimeException("change data error");
        }
    }

    /**
     * 将字符串转化为日期
     *
     * @param style 转化格式
     * @return 日期
     */
    public Date toDate(String style) {
        if (!Objects.equals(this.strValue, "")) {
            SimpleDateFormat dateFormat = new SimpleDateFormat(style);
            try {
                return dateFormat.parse(this.strValue);
            } catch (ParseException e) {
                throw new RuntimeException("to data error" + e);
            }
        } else {
            throw new RuntimeException("to data error");
        }
    }

    public int getIntValue() {
        return intValue;
    }

    public String getStrValue() {
        return strValue;
    }

    public boolean getBoolValue() {
        return boolValue;
    }

    public Map<String, JSONObject> getObjValue() {
        return objValue;
    }

    public List<JSONObject> getArrays() {
        return arrays;
    }

    public double getDouValue() {
        return douValue;
    }

    public long getLongValue() {
        return longValue;
    }

    public byte getByteValue() {
        return Byte.parseByte(String.valueOf(this.intValue));
    }

    public char getCharValue() {
        return this.strValue.charAt(0);
    }

    public short getShortValue() {
        return Short.parseShort(String.valueOf(this.intValue));
    }

    public float getFloatValue() {
        return Float.parseFloat(String.valueOf(this.douValue));
    }
}
